package site.assad.springi18n;

import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Locale;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/1 14:32
 * Description: 测试 spring 对于 i18n的支持
 */
public class I18nTest {

    //测试 ResouceBundleMessageSoucre
    public static void testResouceBundleMessageSoucre(){

        //根据xml配置文件，启动 spring 容器上下文
        String[] configs = {"site/assad/springi18n/beans.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);

        //获取 MessageSource 的 bean
        MessageSource messageSource = ctx.getBean("myResource",MessageSource.class);

        //获取资源包中的国际化信息
        String common1 = messageSource.getMessage("greeting.common",null,Locale.getDefault());
        String common2 = messageSource.getMessage("greeting.common",null, Locale.US);
        String common3 = messageSource.getMessage("greeting.common",null, Locale.CHINA);

        System.out.println(common1+"\n"+common2+"\n"+common3);
        /*output:
        你好!
        How are you!
        你好! */
    }

    //测试 ReloadableResouceBundleMessageSoucre
    public static void testReloadableResourceBundleMessageSouce(){

        String[] configs = {"site/assad/springi18n/beans.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);
        MessageSource messageSource = ctx.getBean("myResource2",MessageSource.class);

        String common1 = messageSource.getMessage("greeting.common",null,Locale.getDefault());
        String common2 = messageSource.getMessage("greeting.common",null, Locale.US);
        String common3 = messageSource.getMessage("greeting.common",null, Locale.CHINA);

        System.out.println(common1+"\n"+common2+"\n"+common3);
        /*output:
        你好!
        How are you!
        你好! */
    }

    /*以容器级别获取国际化信息
    以上2种方式都是将MessageSource作为一个bean注入其他的bean中，
    在实际国际化信息的应用场景中（Spring MVC的页面标签，Controller控制器等），MessageSource 一般作为容器的基础设施向所有的Bean开放，
    因此一般会以容器级别注册MessageSource，详见beans.xml */

    public static void testMessageResource(){

        //运行该测试时，需要将 site/assad/springi18m/beanFmt.xml 中 id="messageSource"的bean注释掉，原因时在一个spring容器中只能存在一个messageSource bean

        String[] configs = {"site/assad/springi18n/beans.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configs);
        //直接通过 ApplicationContext 获取资源
        String common1 = ctx.getMessage("greeting.common",null,Locale.getDefault());
        String common2 = ctx.getMessage("greeting.common",null, Locale.US);
        String common3 = ctx.getMessage("greeting.common",null, Locale.CHINA);

        System.out.println(common1+"\n"+common2+"\n"+common3);

    }


    public static void main(String[] args){
        testResouceBundleMessageSoucre();
        testReloadableResourceBundleMessageSouce();
        testMessageResource();
    }
}
